<?php

/**
 * @author		: Chris Hazekamp
 * @copyright	: 2009
 * @version     : 1.0.0
 * @date        : 1/29/2010 3:40:57 PM
 * @access		: internal
 * @file    	: contact_content.php
 * @site        : 
 */

// Security
if (!defined('PUBLIC_HTML'))
{
	exit;
}

/************************
* CONTACT PAGE IS ACTIVE?
************************/
if (!$config->get('contact_active'))
{
	redirect($main_path);
	exit;
}

$tpl_content = new TemplatePower(PATH_HTML . 'contact_body' . EXT_HTML);
$tpl_content->prepare();

$tpl_content_vars = array(
	'PAGE_TITLE'	=> $page_title,
	'PAGE_CONTENT'	=> $page_content
);
/***********************************
 * MAAK VARIABELEN AAN VOOR DEZE PAGINA
 ***********************************/
$submit		= (isset($_POST['submit'])) 			? true : false;
$captcha	= (isset($_SESSION['captcha_code']))	? $_SESSION['captcha_code'] : '';

// Initiate config vars
$contact			= array();
$contact['email']	= ($config->get('contact_email') != '') ? $config->get('contact_email')		: 'info@nagelstudiojulien.nl';
$contact['name']	= ($config->get('contact_name') != '')	? $config->get('contact_name')		: 'Cynthia Kreffer';

/***********************************
 * CONTACT FORMULIER VERSTUREN
 ***********************************/
if ($submit)
{	
	// Define and secure inputed data
	$contact_id			= NULL;
	$contact_datetime	= date('d-m-Y H:i');
	$contact_email		= $db->sql_injection($_POST['email']);
	$contact_subject	= $db->sql_injection($_POST['subject']);
	$contact_message	= $db->sql_injection($_POST['message']);
	$contact_name		= $db->sql_injection($_POST['name']);
	$contact_ip			= $_SERVER['REMOTE_ADDR'];
	$contact_show_id	= '1';
	//$contact_captcha	= md5($_POST['captcha']);
	
	// Generate error message
	/*$error_message  = '';
	$error_message .= (!$validator->email($contact_email))	? '- U heeft geen geldig e-mail adres ingevuld<br />'	: '';
	$error_message .= (empty($contact_subject))				? '- U heeft geen onderwerp opgegeven<br />' 			: '';
	$error_message .= (empty($contact_message))				? '- U heeft geen bericht ingevuld<br />'				: '';
	$error_message .= (empty($contact_name))				? '- U heeft geen naam ingevuld<br />'					: ''; */
	
	
	// Put all inputed data in an array and prepare them for insertion and mailing
	$post = array(
		'con_id'		=> $contact_id,
		'con_datetime'	=> $contact_datetime,
		'con_email'		=> $contact_email,
		'con_subject'	=> $contact_subject,
		'con_message'	=> $contact_message,
		'con_name'		=> $contact_name,
		'con_ip'		=> $contact_ip,
		'con_show_id'	=> $contact_show_id,
	);
	
		// Insert into database
		$add_contact = $db->sql_insert_row(TABLE_CONTACT_MESSAGES, $post);
		
		// Get the ID of the message
		$new_contact_id	= $db->sql_insert_id();
		
		// Delete the duplicate
		$delete_contact = $db->sql_delete_row(TABLE_CONTACT_MESSAGES, 'con_id', $new_contact_id);
		
		// Build E-mail message body
		$message_body =
		'
		<html>
		<head>
			<title>'.$post['con_subject'].'</title>
		</head>
		<body>
			<table>
			    <tr>
			     	<td>Afzender:</td>
			      	<td>'.$post['con_name'].' ['.$post['con_email'].']</td>
			    </tr>
			    <tr>
			    	<td>Datum:</td>
			    	<td>'.$post['con_datetime'].'</td>
			    </tr>
			    <tr>
			    	<td>Onderwerp:</td>
			    	<td>'.$post['con_subject'].'</td>
			    </tr>
			    <tr>
			    	<td>Bericht:</td>
			    	<td>'.nl2br($post['con_message']).'</td>
			    </tr>
		 	</table>
		</body>
		</html>
		';
		
		// Send e-mail
		$send_mail = send_mail($config->get('contact_email'), $config->get('contact_name'), $post['con_name'], $post['con_email'], $post['con_subject'], $message_body);
		exit;
	}
else
{
	$post = array(
		'con_id'		=> NULL,
		'con_datetime'	=> '',
		'con_email'		=> '',
		'con_subject'	=> '',
		'con_message'	=> '',
		'con_name'		=> '',
		'con_ip'		=> '',
		'con_show_id'	=> '',
	);
}

/***********************************
 * CONTENT GENEREREN
 ***********************************/
 $tpl_content->assign($tpl_content_vars);
?>